const {createAmmeterServer, createWaterMeterServer, createPressureMeterServer, createTemperatureMeterServer,
    createTrifurcateMeterServer, createIndicatorServer
} = require("../service/meterServer");
const {createMonitor , getTypeMeterInfo, getSingleMonitorInfo, getMonitorInfo, quaryMeter, setImgData, setIndicator} = require("../service/monitorServer");
const {getIncrementCounter, readUserToken} = require("../service/interactionServer");



// 创建电表
exports.createAmmeter = async (req, res) => {
    const { alias } = req.body;
    const token = req.headers['authorization'];
    const username = await readUserToken(token)
    if (!alias) {
        return res.status(400).json({ success: false, message: '电表是必需的' });
    }
    try {
        const event = await createAmmeterServer(alias,username)
        res.json({ success: true, message: '电表创建成功', Ammeter_id: event.id });
    } catch (error) {
        res.status(500).json({ success: false, message: '创建电表时发生错误' });
    }
};

// 创建 水表
exports.createWaterMeter = async (req, res) => {
    const { alias , unit } = req.body;
    const token = req.headers['authorization'];
    const username = await readUserToken(token)
    if (!alias || !unit ) {
        return res.status(400).json({ success: false, message: '水表别名 与 单位是必需的' });
    }
    try {
        const event = await createWaterMeterServer(alias , unit ,username )
        res.json({ success: true, message: '水表创建成功', Ammeter_id: event.id });
    } catch (error) {
        res.status(500).json({ success: false, message: '创建水表时发生错误' });
    }
};


// 创建 压力表
exports.createPressureMeter = async (req, res) => {
    const { alias, unit ,range  } = req.body;
    const token = req.headers['authorization'];
    const username = await readUserToken(token)

    if (!alias || !range || !unit ) {
        return res.status(400).json({ success: false, message: '压力表别名 与 单位是必需的' });
    }
    try {
        const event = await createPressureMeterServer(alias , unit ,range , username )
        res.json({ success: true, message: '压力表创建成功', Ammeter_id: event.id });
    } catch (error) {
        res.status(500).json({ success: false, message: '创建压力表时发生错误' });
    }
};

// 创建 温度表
exports.createTemperatureMeter = async (req, res) => {
    const { alias, unit ,range  } = req.body;
    const token = req.headers['authorization'];
    const username = await readUserToken(token)

    if (!alias) {
        return res.status(400).json({ success: false, message: '温度表别名 与 单位是必需的' });
    }
    try {
        const event = await createTemperatureMeterServer( alias, unit ,range ,username )
        res.json({ success: true, message: '温度表创建成功', Ammeter_id: event.id });
    } catch (error) {
        res.status(500).json({ success: false, message: '创建温度表时发生错误' });
    }
};

// 创建 三叉表
exports.createTrifurcateMeter = async (req, res) => {
    const { alias, unit ,range  } = req.body;
    const token = req.headers['authorization'];
    const username = await readUserToken(token)

    // console.log(username);

    if (!alias || !range || !unit ) {
        return res.status(400).json({ success: false, message: '三叉表别名 与 单位是必需的' });
    }
    try {
        const event = await createTrifurcateMeterServer(alias , unit ,range , username )
        res.json({ success: true, message: '三叉表创建成功', Ammeter_id: event.id });
    } catch (error) {
        res.status(500).json({ success: false, message: '创建三叉表时发生错误' });
    }
};

// 创建 指示器
exports.createIndicator = async (req, res) => {
    const { alias  } = req.body;
    const token = req.headers['authorization'];
    const username = await readUserToken(token)

    // console.log(username);

    if (!alias ) {
        return res.status(400).json({ success: false, message: '指示器别名 与 单位是必需的' });
    }
    try {
        const event = await createIndicatorServer(alias,username)
        res.json({ success: true, message: '指示器创建成功', Ammeter_id: event.id });
    } catch (error) {
        res.status(500).json({ success: false, message: '创建指示器时发生错误' });
    }
};


// 创建 液位
exports.createLiquidLevelMeter = async (req, res) => {
    const { alias  } = req.body;
    const token = req.headers['authorization'];
    const username = await readUserToken(token)

    // console.log(username);

    if (!alias ) {
        return res.status(400).json({ success: false, message: '液位别名 是必需的' });
    }
    try {
        const event = await createIndicatorServer(alias,username)
        res.json({ success: true, message: '液位创建成功', Ammeter_id: event.id });
    } catch (error) {
        res.status(500).json({ success: false, message: '创建液位时发生错误' });
    }
};




// 创建监控
exports.createMonitor = async (req, res) => {
    const { type, meterId, monitorId } = req.body;
    const token = req.headers['authorization'];
    // 验证输入
    if (!meterId || !monitorId) {
        return res.status(400).json({ success: false, message: '类型 、 表ID 和  监控ID 是必需的' });
    }

    const event = await createMonitor(type, meterId, monitorId,token)

    res.json(event);

}





// 获取某类型的所有表信息
exports.getTypeMeter = async (req, res) => {
    const { type } = req.query;
    const event = await getTypeMeterInfo(type)
    res.json(event);
};


// 获取 单个表的监控 详细信息
exports.getMeterInfo = async (req, res) => {
    const { type , meterId } = req.body;
    const event  = await getSingleMonitorInfo(type, meterId)
    res.json(event);
};


// 刷新 详细信息
exports.getNewestMonitor = async (req, res) => {
    const { monitorId } = req.query;
    const event  = await getMonitorInfo(monitorId)
    res.json(event);
}



// 搜索框 搜索
exports.quaryMeter = async (req, res) => {
    const { meterId , alias , type ,monitorId } = req.body;
    const event = await quaryMeter(type, meterId, alias, monitorId)
    res.json(event);
};


// 获取图片
exports.setImgData = async (req, res) => {
    const { monitorId } = req.query;
    console.log(monitorId);
    const event = await setImgData(monitorId)
    res.json(event);
}

// 标注
exports.setIndicator = async (req, res) => {
    const { monitorId , data  } = req.body;
    // console.log("12121",monitorId , data)
    const event = await setIndicator(monitorId , data)
    res.json(event);
}

// 获取计数信息
exports.getIncrement = async (req, res) => {
    const token = req.headers['authorization'];
    const username = await readUserToken(token)
    const result = await getIncrementCounter(username);
    if (result) {
        res.status(200).json(result);
    } else {
        res.status(500).json({ message: 'Failed to fetch increment numbers.' });
    }
};





//  TODO   更换监控(  要删除原本信息，在新建（保护redis压力） )

//  TODO   修改表信息

//  TODO   删除表(标记删除？？ ai运行嘛)



